package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelContentService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户APP资源控制器
 */
@RestController
@RequestMapping("travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelContentService travelContentService;

    @Autowired
    private ITravelCommendService commendService;
    @PostMapping
    public JSONResult save(Travel travel){
        JSONResult result = new JSONResult();
        try {

            travelService.saveOrUpdate(travel);
        } catch (Exception e) {
            result.mark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
    @GetMapping("{id}")
    public Travel getTravelById(@PathVariable Long id){
        Travel travel = travelService.getTravelById(id);
        return travel;
    }
    @PutMapping("{id}")
    public JSONResult update(Travel travel){
        travelService.saveOrUpdate(travel);
        return new JSONResult();
    }

    @GetMapping
    public PageInfo query(@ModelAttribute("qo") TravelQueryObject qo, Model model){
        //按发布时间倒序排列
        qo.setOrderBy("t.releaseTime desc");
        //设置查询条件为  已发布状态
        qo.setState(Travel.STATE_RELEASE);
        return travelService.query(qo);
    }
    @GetMapping("commends")
    public PageInfo queryCommend(TravelCommendQueryObject qo){
        qo.setOrderBy("commend.schedule desc");
        qo.setBeforeToday(true);//获取今天之前的数据
        return commendService.selectForAppList(qo);
    }

    /**
     * 获取该用户被点赞的未读游记
     * @param qo
     * @return
     */
    @GetMapping("doGoodTravel")
    public PageInfo queryDoGood(@ModelAttribute("qo") TravelQueryObject qo){
        PageInfo pageInfo = travelService.selectTravelForDoGood(qo);
        return pageInfo;
    }
}
